<?php
declare(strict_types=1);

namespace Modules\Production\Http\Controllers;

use Catch\Base\CatchController as Controller;
use App\Services\RedisLockService;
use Modules\Production\Models\ProductionProcessDetail;
use Illuminate\Http\Request;use Modules\Production\Repository\ProcessDetailRepository;


class ProductionProcessDetailController extends Controller
{
    public function __construct(
        protected readonly ProductionProcessDetail $model
    ){}

    /**
     * @param Request $request
     * @return mixed
     */
    public function index(): mixed
    {
        return $this->model->getList();
    }

    /**
     * 工序报工提交6
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request)
    {
        $postData = $request->post();

        $key = 'PROCESS_SUBMIT_' . $postData['process_id'];
        $lock = new RedisLockService($key);
        if(!$lock->lock()){
            throw new \Exception('当前工序报工提交处理中，请稍后再试');
        }

        try{
            return ProcessDetailRepository::save($request->post());
        }catch (\Exception $e){
            throw $e;
        } finally {
            $lock->unlock();
        }

    }

    /**
     * @param $id
     * @return mixed
     */
    public function show($id)
    {
        return $this->model->firstBy($id);
    }

    /**
     * @param Request $request
     * @param $id
     * @return mixed
     */
    public function update($id, Request $request)
    {
        return $this->model->updateBy($id, $request->all());
    }

    /**
     * 删除工序报工明细
     * @param $id
     * @return mixed
     */
    public function destroy($id)
    {
        return ProcessDetailRepository::deleteBy($id);
    }

    public function getByQrcode(Request $request){
        return ProcessDetailRepository::getById($request->get('qrcode'));
    }
}
